package org.rcgwt.client.ui.validator.impl;

import java.util.Date;
import java.util.Map;

import org.rcgwt.client.ui.validator.IClientValidatorContext;
import org.rcgwt.client.ui.validator.IFormatter;

import com.google.gwt.i18n.client.DateTimeFormat;

/**
 * A date formatter that formats textually a date (according to the local
 * format)
 * 
 * @author Elian ORIOU
 * 
 */

public class DateFormatter implements IFormatter {

	private final DateTimeFormat dateTimeFormat;

	public DateFormatter(Map<String, String> parameters) {

		dateTimeFormat = DateConverter.computeDateTimeFormat(parameters);
	}

	@Override
	public String format(IClientValidatorContext context, String date) {

		// Parsing with the current format
		Date newDate = dateTimeFormat.parse(date);
		if (newDate == null) {
			return null;
		}

		return dateTimeFormat.format(newDate);
	}
}
